package com.zahb.qadx.util;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.widget.TextView;

/**
 * EditText包装类
 * Created by zhoudawei on 2016/12/6.
 */
public final class EditTextWrapper {

    private TextView mEditText;

    private Drawable mDrawableStart;
    private Drawable mDrawableTop;
    private Drawable mDrawableEnd;
    private Drawable mDrawableBottom;

    private TextWatcher mTextWatcher;

    private onDrawableRightClickListener mOnDrawableRightClickListener;

    @SuppressLint("ClickableViewAccessibility")
    EditTextWrapper(TextView editText) {
        mEditText = editText;
        initDrawables();
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                if (mTextWatcher != null)
                    mTextWatcher.beforeTextChanged(s, start, count, after);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (mTextWatcher != null)
                    mTextWatcher.onTextChanged(s, start, before, count);
            }

            @Override
            public void afterTextChanged(Editable s) {
                setDrawables(s.toString());
                if (mTextWatcher != null)
                    mTextWatcher.afterTextChanged(s);
            }
        });
        mEditText.setOnTouchListener((view, event) -> checkDrawableRightClick(event));
    }

    private void initDrawables() {
        Drawable[] drawables = mEditText.getCompoundDrawablesRelative();
        mDrawableStart = drawables[0];
        mDrawableTop = drawables[1];
        mDrawableEnd = drawables[2];
        mDrawableBottom = drawables[3];
    }

    private void setDrawables(String text) {
        if (TextUtils.isEmpty(text))
            mEditText.setCompoundDrawablesRelative(mDrawableStart, mDrawableTop, null, mDrawableBottom);
        else
            mEditText.setCompoundDrawablesRelative(mDrawableStart, mDrawableTop, mDrawableEnd, mDrawableBottom);
    }

    private boolean checkDrawableRightClick(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            Drawable[] drawables = mEditText.getCompoundDrawablesRelative();
            final int centerY = mEditText.getPaddingTop() +
                    (mEditText.getHeight() - mEditText.getPaddingBottom() - mEditText.getPaddingTop()) / 2;
            if (drawables[2] != null &&
                    event.getX() > mEditText.getWidth() - mEditText.getPaddingEnd() - drawables[2].getMinimumWidth() / 3f * 4 &&
                    event.getY() > centerY - drawables[2].getIntrinsicHeight() / 3f * 2 &&
                    event.getY() < centerY + drawables[2].getIntrinsicHeight() / 3f * 2) {
                if (mOnDrawableRightClickListener != null)
                    mOnDrawableRightClickListener.onDrawableRightClick();
            }
        }
        return false;
    }

    void setDrawableRight(Drawable drawableRight) {
        if (drawableRight != null)
            drawableRight.setBounds(0, 0, drawableRight.getMinimumWidth(), drawableRight.getMinimumHeight());
        mDrawableEnd = drawableRight;
        setDrawables(mEditText.getText().toString());
    }

    public void setTextWatcher(TextWatcher textWatcher) {
        mTextWatcher = textWatcher;
    }

    void setOnDrawableRightClickListener(onDrawableRightClickListener listener) {
        mOnDrawableRightClickListener = listener;
    }

    interface onDrawableRightClickListener {
        void onDrawableRightClick();
    }
}
